﻿using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;
using TXLooker.Hooks;
using TXLooker.Properties;
using KeyEventArgs=System.Windows.Forms.KeyEventArgs;

namespace TXLooker.UI
{
    public partial class Registration_Dialog : Form
    {
        // Methods
        private Registration_Dialog()
        {
            this.m_seconds = 10;
            this.components = null;
            this.InitializeComponent();
        }

        private Registration_Dialog(string productState, int daysLeft)
            : this()
        {
            this.m_lVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            this.m_lProductState.Text = productState;
            this.m_lDaysLeft.Text = daysLeft.ToString();
        }

        public Registration_Dialog(string productState, int days, Microsoft.Office.Interop.Outlook.Application application, bool showRegistrateButton, bool licenceExpired)
            : this(productState, days)
        {
            this.m_btnRegistration.Visible = showRegistrateButton;
            this.m_llLicenceEmail.Visible = licenceExpired;
            this.m_llLicenceEmail.Enabled = licenceExpired;
            this.m_tbLicenceExpired.Visible = licenceExpired;
            this.m_lregistrationKey.Visible = licenceExpired;
            this.m_Application = application;
        }

        private void OnClosing(object sender, FormClosingEventArgs e)
        {
            this.m_keyboardHook.RemoveHook(false);
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            if (!e.Alt)
            {
            }
        }

        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            if (!e.Alt)
            {
            }
        }

        public event EventHandler CreateEmail;

        private void InvokeCreateEmail(EventArgs e)
        {
            EventHandler createEmailHandler = CreateEmail;
            if (createEmailHandler != null) createEmailHandler(this, e);
        }

        private void OnLicenceEmailClick(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.CreateEMail = true;

            InvokeCreateEmail(EventArgs.Empty);

            base.Close();
        }

        private void OnLinkeClick(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://teamexpand.com");
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.m_lregistrationKey.Text = "Your activation key is: " + this.ActivationKey;
            this.m_btnOk.Text = "OK";
            this.m_btnOk.Enabled = false;
            this.m_timer = new Timer();
            this.m_timer.Interval = 0x3e8;
            this.m_timer.Tick += new EventHandler(this.OnTimerTick);
            this.m_timer.Enabled = true;
        }

        private void OnLoad(object sender, EventArgs e)
        {
            this.m_keyboardHook = new LLKeyboardHook();
            this.m_keyboardHook.KeyUp += new LLKeyboardHook.KeyEventHandler(this.OnKeyUp);
            this.m_keyboardHook.KeyDown += new LLKeyboardHook.KeyEventHandler(this.OnKeyDown);
            this.m_keyboardHook.InstallHook(false);
        }

        private void OnOk(object sender, EventArgs e)
        {
            base.Close();
        }

        private void OnRegistration(object sender, EventArgs e)
        {
            new RegistrationForm(this.m_Application).Show();
            base.Close();
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            this.m_btnOk.Text = string.Format("{0} OK", this.m_seconds);
            this.m_seconds--;
            if (this.m_seconds == 0)
            {
                this.m_btnOk.Enabled = true;
                this.m_btnOk.Text = "OK";
                this.m_timer.Stop();
            }
        }

        // Properties
        private string ActivationKey
        {
            get
            {
                return (string)Registry.CurrentUser.CreateSubKey(Resources.ACTIVATION_REG_KEY_PATH).GetValue(Resources.ACTIVATION_REG_KEY, string.Empty);
            }
        }

        public bool CreateEMail
        {
            get
            {
                return this.m_bCreateEMail;
            }
            private set
            {
                this.m_bCreateEMail = value;
            }
        }

    }
}
